import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
   /**
 * This enum encapsulates all the sound effects of a game, so as to separate the sound playing
 * codes from the game codes.
 * 1. Define all your sound effect names and the associated wave file.
 * 2. To play a specific sound, simply invoke SoundEffect.SOUND_NAME.play().
 * 3. You might optionally invoke the static method SoundEffect.init() to pre-load all the
 *    sound files, so that the play is not paused while loading the file for the first time.
 * 4. You can use the static variable SoundEffect.volume to mute the sound.
 */
public final class SoundEffect {
   public static Volume volume = Volume.LOW;
   private static Clip clip;
   private static URL url;
   public SoundEffect()
	{
		//now we exists
	}
   public static enum Volume {
      MUTE, LOW, MEDIUM, HIGH
   }
   // Constructor to construct each element of the enum with its own sound file.
   public static boolean play1(String soundFileName) {
      try {
         // Use URL (instead of File) to read from disk and JAR.
         url = Main.input.getClass().getClassLoader().getResource("sounds/"+soundFileName);
        
         
         // Set up an audio input stream piped from the sound file.
         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
         // Get a clip resource.
         clip = AudioSystem.getClip();
         // Open audio clip and load samples from the audio input stream.
         clip.open(audioInputStream);
      } catch (UnsupportedAudioFileException e) {
         e.printStackTrace();
         return false;
      } catch (IOException e) {
         e.printStackTrace();
         System.out.println("ERROR PLAYING SOUND "+soundFileName+"  "+url);
         return false;
      } catch (LineUnavailableException e) {
         e.printStackTrace();
         System.out.println("ERROR PLAYING SOUND "+soundFileName+"  "+url);
         return false;
      } catch (NullPointerException e){
    	  e.printStackTrace();
    	  System.out.println("ERROR PLAYING SOUND "+soundFileName+"  "+url);
    	  return false;
      }
      
      return true;
   }
// Play or Re-play the sound effect from the beginning, by rewinding.
   public static void play(String name) {
		if(play1(name)){
			if (volume != Volume.MUTE) {
				if (clip.isRunning())
				clip.stop();   // Stop the player if it is still running
				clip.setFramePosition(0); // rewind to the beginning
				clip.start();     // Start playing
			}
		}
	  
   }
}